
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.rms.RecordStoreException;

public class HighScoreScreen extends GameCanvas implements CommandListener {

    private int space = 20;
    private Main iMain;
    private MenuScreen iMenuScreen;
    private Command cmdBack;
    private Image bgHighScore;
    private Player clickSound;

    public HighScoreScreen(Main main, MenuScreen menu) {
        super(true);
        setFullScreenMode(true);

        this.iMain = main;
        this.iMenuScreen = menu;
        cmdBack = new Command("OK", Command.BACK, 0);
        addCommand(cmdBack);
        setCommandListener(this);
    }

    public void Start() {
        try {
            //moveSound = Manager.c

            InputStream in2 = getClass().getResourceAsStream("/click.wav");
            clickSound = Manager.createPlayer(in2, "audio/x-wav");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        try {
            bgHighScore = Image.createImage("HighScore.png");
        } catch (Exception e) {
        };
        try {
            iMain.doRaMang();
        } catch (RecordStoreException ex) {
        } catch (IOException ex) {
        }

        iMain.sapXepDiem();

        String[] arNickName = iMain.getArNickName();
        int[] arScore = iMain.getArScore();
        int nHighScore = iMain.get_nHighScore();
        int iHighScore = iMain.get_iHighScore();
        int n;
        if (iHighScore > nHighScore) {
            n = nHighScore;
        } else {
            n = iHighScore;
        }

        Graphics g = getGraphics();

        g.drawImage(bgHighScore, 0, 0, 0);

        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
        for (int i = 0; i < n; i++) {
            g.drawString(arNickName[i], 30, 60 + i * space + 10, Graphics.TOP | Graphics.LEFT);
            g.drawString(Integer.toString(arScore[i]), 210, 60 + i * space + 10, Graphics.TOP | Graphics.RIGHT);
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c.getCommandType() == Command.BACK) {
            if (OptionScreen.getChon() == 1) {
                try {
                    clickSound.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }
            Display.getDisplay(iMain).setCurrent(iMenuScreen);
        }
    }
}
